package cn.attackme.myuploader.controller;

import org.apache.catalina.User;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/tesT")
public class TestController {
    @RequestMapping(value="start", method = RequestMethod.POST)
    public  String test(@RequestParam String canshu){
        System.out.println("upload ok:"+canshu);
        return " test :" + canshu;
    }


    @RequestMapping(value="pause", method = RequestMethod.POST)
    public  String test2(@RequestParam String password,@RequestParam String name){
        System.out.println("upload ok:"+password+"--"+name+":");
        return " test :" + password+"--"+name;
    }

    @RequestMapping(value="resume", method = RequestMethod.POST)
    public  Map<Object,Object> test3(@RequestBody Map<Object,Object> map){
        System.out.println(map);
        System.out.println(map.get("username"));
        return map;
    }


    @PostMapping("uploadFiles")
    public String uploadFile(@RequestParam(value = "access_token") String access_token,@RequestParam(value = "file") MultipartFile file) {
         String name = file.getName();
         String fileType = file.getContentType();
         System.out.println(name+"---"+access_token);
        System.out.println("fileType:"+fileType);
        long size= file.getSize();
        String contentType= file.getContentType();
        String orgFilename= file.getOriginalFilename();
        System.out.println("size:"+size);
        System.out.println("contentType:"+contentType);
        System.out.println("name:"+name);
        System.out.println("orgFilename:"+orgFilename);

        String suffix = orgFilename.substring(orgFilename.lastIndexOf("."));//后缀

        String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();

        File dest =   new File("E:/test/"+uuid+suffix);
        try {
            file.transferTo(dest);

            return dest.getCanonicalPath();//文件的绝对路径
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        return "failure";
    }
}
